/**
 * Created by Administrator on 2015/12/29.
 */
angular.module('gywm.controller',[])
  .controller('gywmCtrl',['$scope','gywmService','$state','$localstorage','$ionicPopup','gywmService',function($scope,gywmService,$state,$localstorage,$ionicPopup,gywmService){

    $scope.$on('$ionicView.enter', function (event, data) {
      if(!$localstorage.get("_LOGIN_PARAM")){
        $scope.showAlert = function() {
          var alertPopup = $ionicPopup.alert({
            title: '警告',
            template: '当前用户已经注销，请重新登录',
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
          alertPopup.then(function(res) {
            $state.go("login");
          });
        };
        $scope.showAlert();
      }
      if($localstorage.get("LOG_FLAG")=="1"){
        gywmService.insertLog("关于我们").then(function(data){
          console.log("插入日志"+angular.toJson(data));
        },function(error){
          console.log("插入日志失败，请联系管理员");
        });
      }


    })

    $scope.doClick=function(){
      //客户校验接口
      gywmService.doClick().then(
        function(data){
          //console.log(data);
        },function(error){
          //console.log(error);
        });
    }

    //直接操作就好了，感觉也用不到service
    $scope.doLogout=function()
    {

      //不管成功还是失败都要清空保存的数据
      gywmService.doLogout().then(function(data){

        //console.log(data);
        if(data.respCode=="0") {
          clearMemery();
        }
        else{
          var alertPopup = $ionicPopup.alert({
            title: '提示',
            template: '请求失败，请重新注销'+success.respDesc,
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
        }
      },function(err){
        var alertPopup = $ionicPopup.alert({
          title: '提示',
          template: '网络不稳定，请重新注销',
          okText: '确定', // String (默认: 'OK')。OK按钮的文字。
          okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
        });
      });



    }

    function clearMemery()
    {
      $localstorage.set("_LOGIN_PARAM","");
      $localstorage.set("EPARCHY_CODE","");
      $localstorage.set("CUC_DEPART_CODE","");
      $localstorage.set("STAFF_ID","");
      $localstorage.set("DEPART_ID","");
      $localstorage.set("CUC_EPARCHY_CODE","");
      $localstorage.set("DEPART_NAME","");
      $localstorage.set("PASSWORD","");

      $state.go('login');
    }
  }]);
